package com.lottery.im.module.service.presenter;

import android.app.Activity;
import com.lottery.im.MyApplication;
import com.lottery.im.api.ApiClient;
import com.lottery.im.api.observer.BaseObserver;
import com.lottery.im.api.observer.ShowTipObserver2;
import com.lottery.im.api.result.entity.CanReceiveData0;
import com.lottery.im.api.result.entity.redpacket.CanReceiveData2;
import com.lottery.im.api.result.entity.redpacket.Type3SQ;
import com.lottery.im.base.BaseFragmentPresenter;
import com.lottery.im.module.service.view.Type3SQListView;
import com.lottery.im.util.ToastUtils;
import com.trello.rxlifecycle2.LifecycleProvider;
import com.trello.rxlifecycle2.android.FragmentEvent;
import io.reactivex.android.schedulers.AndroidSchedulers;
import io.reactivex.schedulers.Schedulers;
import java.util.concurrent.TimeUnit;

/**
 * 作者：cyy
 * 时间：2018/9/25。
 */
public class Type3SQListPresenter extends BaseFragmentPresenter<Type3SQListView>{
  public Type3SQListPresenter(Activity activity,
      Type3SQListView iView,
      LifecycleProvider<FragmentEvent> provider) {
    super(activity, iView, provider);
  }

  String token = MyApplication.getInstance().getToken();
  String userId = MyApplication.getInstance().getUserInfo().getUserId();
  public void getType3SQList(){
    ApiClient.getApiRetrofitInstance()
        .getType3SQList(userId,token)
        .throttleFirst(2, TimeUnit.SECONDS)
        .subscribeOn(Schedulers.io())
        .observeOn(AndroidSchedulers.mainThread())
        .compose(provider.bindToLifecycle())
        .subscribe(new BaseObserver<Type3SQ>() {
          @Override public void onStart() {

          }

          @Override public void onSuccess(Type3SQ type3SQ) {
            iView.loadType3SQSuccess(type3SQ);
          }

          @Override public void onFail(String code, String msg) {
          }

          @Override public void onException(String msg) {

          }

          @Override public void onFinish() {

          }
        });
  }

  public void loadCanReceive(String type){
    ApiClient.getApiRetrofitInstance()
        .loadCanReceive2(userId,token,type)
        .throttleFirst(2, TimeUnit.SECONDS)
        .subscribeOn(Schedulers.io())
        .observeOn(AndroidSchedulers.mainThread())
        .compose(provider.bindToLifecycle())
        .subscribe(new ShowTipObserver2<CanReceiveData2>(activity) {
          @Override public void onSuccess(CanReceiveData2 receiveData2, String returnCode) {
            iView.loadCanReciveData1Success(receiveData2,returnCode);
          }
        });
  }

  public void onReceiveRedPacket(String type){
    ApiClient.getApiRetrofitInstance()
        .ReceiveSignReword0(userId,token,type)
        .throttleFirst(2,TimeUnit.SECONDS)
        .subscribeOn(Schedulers.io())
        .observeOn(AndroidSchedulers.mainThread())
        .compose(provider.bindToLifecycle())
        .subscribe(new BaseObserver<CanReceiveData0.ReturnMapBean>() {
          @Override public void onStart() {
            showLoadingProgress("正在加载");
          }

          @Override public void onSuccess(CanReceiveData0.ReturnMapBean returnMapBean) {
            iView.receiveRedPacketSuccess();
            closeLoadingProgress();
          }

          @Override public void onFail(String code, String msg) {
            ToastUtils.showToast(activity,msg);
            closeLoadingProgress();
          }

          @Override public void onException(String msg) {
            closeLoadingProgress();
          }

          @Override public void onFinish() {
            closeLoadingProgress();
          }
        });
  }
}
